package com.shujia;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import redis.clients.jedis.Jedis;

import java.util.HashMap;
import java.util.Map;

public class Demo05Hash {
    Jedis jedis;

    @Before
    // 在@Test注解方法之前会运行一次
    public void init() {
        jedis = new Jedis("master", 6379);
    }

    @Test
    public void hSet() {
        HashMap<String, String> stuMap = new HashMap<>();
        stuMap.put("name", "张三");
        stuMap.put("age", "22");
        stuMap.put("clazz", "理科五班");

        jedis.hset("student", stuMap);
    }

    @Test
    public void hGet() {
        System.out.println(jedis.hget("student", "clazz"));
    }

    @Test
    public void hGetAll() {
        for (Map.Entry<String, String> student : jedis.hgetAll("student").entrySet()) {
            System.out.println(student.getKey() + "," + student.getValue());
        }
    }

    @Test
    public void getKeys() {
        for (String filed : jedis.hkeys("student")) {
            System.out.println(filed);
        }
    }

    @Test
    public void getVals() {
        for (String val : jedis.hvals("student")) {
            System.out.println(val);
        }
    }

    @Test
    public void hLen() {
        System.out.println(jedis.hlen("student"));
    }


    @After
    // 在@Test注解方法之后会运行一次
    public void close() {
        jedis.close();
    }
}
